﻿using System;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace EmptyWallGallery.FrontEnd.inside.Controls
{
    public partial class ProductCategoryEdit : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadCatDetail();
        }

        public void LoadCatDetail()
        {
            if (Request.QueryString["CatID"] != null)
            {
                if (Core.Utilities.Common.Instance.IsNumeric(Request.QueryString["CatID"]))
                {
                    var catDetail = Core.BLL.Categories.Instance.GetById(int.Parse(Request.QueryString["CatID"]));

                    txtCatIndex.Text = Convert.ToString(catDetail.CatIndex);
                    txtDesc.Text = catDetail.Description;
                    txtTitle.Text = catDetail.Title;
                    DrpDisplay.SelectedValue = Convert.ToString(catDetail.SiteId);

                    LoadParentCat(catDetail.SubCatx == 0
                                      ? Convert.ToString(catDetail.SubCat)
                                      : Convert.ToString(catDetail.SubCatx));
                }
                else
                {
                    Response.Redirect("default.aspx");
                }

            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }

        public void LoadParentCat(string catId)
        {
            //DropDownList1.DataSource = Core.BLL.Categories.Instance.GetParentCat(0, 0, 2);
            //DropDownList1.DataTextField = "Title";
            //DropDownList1.DataValueField = "CatID";
            //DropDownList1.DataBind();

            //DropDownList1.Items.Insert(0, "Không có");
            //DropDownList1.SelectedValue = "None";

            cboCategory.InnerHtml = Core.BLL.Categories.Instance.WriteCategoryInside(2,
                                                                                     Convert.ToInt32(
                                                                                         Request.QueryString["CatID"]),
                                                                                     catId);
        }


        protected void BtnUpdateClick(object sender, EventArgs e)
        {
            try
            {
                var subCat = 0;
                var subCatx = 0;

                if (Request.Form["cboCategory"] != "None")
                {
                    var str = Regex.Split(Request.Form["cboCategory"], ",");
                    subCat = int.Parse(str[0]);
                    subCatx = int.Parse(str[1]);
                    if (subCatx != 0)
                    {
                        subCatx = int.Parse(str[0]);
                        subCat = int.Parse(str[1]);                        
                    }
                }

                Core.BLL.Categories.Instance.Update(int.Parse(Request.QueryString["CatID"]), txtTitle.Text.Trim(),
                                                    txtDesc.Text.Trim(),
                                                    Core.Utilities.Common.Instance.Utf8ToAscii(txtTitle.Text.Trim()),
                                                    int.Parse(txtCatIndex.Text.Trim()),
                                                    subCat, subCatx, Convert.ToInt32(DrpDisplay.SelectedValue));

                lblError.InnerHtml += Core.BLL.Categories.Instance.WriteCategory(true);
                lblError.InnerHtml += "OK";
                Response.Redirect("default.aspx");
            }
            catch (Exception ex)
            {
                lblError.InnerHtml = ex.Message;
                //Response.Write(ex.Message);
            }
        }
    }
}